import 'package:flutter/material.dart';
import 'package:open_source_china/api/user.dart';
import 'package:open_source_china/constants/size_list.dart';
import 'package:open_source_china/model/user_detial.dart';
import 'package:open_source_china/utils/DataUtils.dart';
import 'package:open_source_china/utils/NetUtils.dart';
import 'package:open_source_china/widget/avatar.dart';
import 'package:flutter/rendering.dart';
import 'package:toast/toast.dart';

class ProfileDetail extends StatefulWidget {
  final int id;
  ProfileDetail(this.id);
  @override
  _ProfileDetailState createState() => _ProfileDetailState();
}

class _ProfileDetailState extends State<ProfileDetail> {
  UserDetail userDetail;
  @override
  void initState() {
    super.initState();
    UserApi.getUserDetail(widget.id).then((user) {
      setState(() {
        this.userDetail = user;
      });
    }).catchError((error) {
      Toast.show("网络错误", context);
    });
  }

  @override
  Widget build(BuildContext context) {
    // debugPaintSizeEnabled = true;
    return Scaffold(
        appBar: AppBar(
          title: Text("我的资料"),
        ),
        body: Container(
            child: userDetail != null
                ? ListView(
                    children: [
                      buildItem(() {
                        print("修改头像");
                      },
                          Text("头像"),
                          Avatar(
                            path: userDetail.portrait,
                            width: SizeList.AVATAR_SIZE,
                          )),
                      buildItem(() {}, Text("昵称"), Text(userDetail?.name)),
                      buildItem(
                          () {}, Text("加入时间"), Text(userDetail?.joinTime)),
                      buildItem(() {}, Text("所在地区"), Text(userDetail.location)),
                      buildItem(
                          () {},
                          Text("开发平台"),
                          Container(
                            width: 230.0,
                            child: Text(
                              userDetail.platformsJoin,
                              textAlign: TextAlign.right,
                            ),
                          )),
                      buildItem(
                          () {},
                          Text("专长领域"),
                          Text(
                            userDetail.expertiseJoin,
                            textAlign: TextAlign.right,
                          )),
                    ],
                  )
                : Center(
                    child: Text("loading..."),
                  )));
  }

  /// 构建每一行
  GestureDetector buildItem(void handler(), Widget title, Widget tailing) {
    return GestureDetector(
        onTap: handler,
        child: Container(
          padding: EdgeInsets.only(left: 15),
          child: Container(
            decoration: BoxDecoration(
                border: Border(
                    bottom: BorderSide(width: 0.5, color: Colors.grey[400]))),
            padding: EdgeInsets.fromLTRB(0, 15, 15, 15),
            child: Row(
              children: [
                title,
                Expanded(child: Text("")),
                tailing,
                Icon(Icons.arrow_right)
              ],
            ),
          ),
        ));
  }
}
